package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: 检查组管理
 * @author chenxys
 * @date 2022/5/19 20:30
 */
@RestController
@RequestMapping(value = "/checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 新增查询组
     * @author chenxys
     * @param checkGroup
     * @param checkItemIds
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/add")
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD') and hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkItemIds){
        try {
            checkGroupService.add(checkGroup,checkItemIds);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 分页查询功能
     * @author chenxys
     * @param queryPageBean
     * @return com.itheima.entity.PageResult
     */
    @RequestMapping(value = "/pageQuery")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY') and hasAuthority('CHECKITEM_QUERY')")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){
        return checkGroupService.pageQuery(queryPageBean);
    }

    /**
     * 根据ID查询检查组
     * @author chenxys
     * @param id
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/findById")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY') and hasAuthority('CHECKITEM_QUERY')")
    public Result findById(Integer id){
        CheckGroup checkGroup = checkGroupService.findById(id);
        if (checkGroup != null){
            return  new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    /**
     * 根据检查组ID查询对应的所有检查项ID
     * @author chenxys
     * @param id
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/findCheckItemIdsByCheckGroupId")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY') and hasAuthority('CHECKITEM_QUERY')")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try{
            List<Integer> checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
        }catch(Exception e){
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 修改检查组
     * @author chenxys
     * @param checkItemIds
     * @param checkGroup
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/edit")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result edit(Integer[] checkItemIds,@RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.edit(checkGroup,checkItemIds);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 删除检查组
     * @author chenxys
     * @param id
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/delete")
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE') and hasAuthority('CHECKITEM_EDIT')")
    public Result delete(Integer id){
        try {
            checkGroupService.delete(id);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 获取所有检查组信息
     * @author chenxys
     * @param
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/findAll")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY') and hasAuthority('CHECKITEM_QUERY')")
    public Result findAll(){
        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        if (checkGroupList != null && checkGroupList.size() > 0){
            //查询检查组成功
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        }
        //查询检查组失败
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }
}
